package urnav.example.com;

import geocoding.ALGOTrip;
import geocoding.CustomNodes;
import graph.ALGOEdge;
import graph.ALGOGraph;
import graph.ALGONode;
import graph.ALGOPlacemarkNode;
import graph.SearchableGraph;

import org.osmdroid.views.overlay.MyLocationOverlay;

import osm.OsmReader;
import routing.Trip;
import android.location.Location;

public class Search {
	String TAG="URNav";
	
	public Trip search(int targetNodeID, ALGOGraph myGraph, OsmReader myOsmReader, Trip myTrip, MyLocationOverlay myLocOverlay){
		int startNodeID= getClosestItem(myGraph, myOsmReader, myTrip, myLocOverlay);  
//		int startNodeID=7;
		ALGONode startNode=myGraph.getNode(startNodeID);
//		Log.i(TAG, String.valueOf(startNodeID));
		ALGONode targetNode=myGraph.getNode(targetNodeID);
//		Log.i(TAG, String.valueOf(targetNodeID));  
	      
	    ((SearchableGraph)myGraph).initSearch(startNode, targetNode);
		while (!((SearchableGraph)myGraph).solutionFound() &&
				!((SearchableGraph)myGraph).noSolution()) {
			
				((SearchableGraph)myGraph).nextStep();
			 
				
			}

			
			
		
	    
	    
	  if (((SearchableGraph)myGraph).solutionFound()) {
//		  Log.i(TAG, "Solution Found");
		  
		
		ALGOTrip trip = ((SearchableGraph)myGraph).computeShortestPath(startNode, targetNode);
		
		String TAG="URNav";
		for (ALGOEdge algoEdge : trip) {
//			Log.i(TAG, "yxc "+((ALGOPlacemarkNode)myGraph.getNode(algoEdge.getSource())).getValue().getName() + " -> " + ((ALGOPlacemarkNode)myGraph.getNode(algoEdge.getSink())).getValue().getName());
			
			// checking if Item is already in the Trip by looking up its ID/ Name in the ArrayList of Trip; returns -1 if not found
			int p=myTrip.getPosition(myOsmReader.getNodes().getPosition(((ALGOPlacemarkNode)myGraph.getNode(algoEdge.getSource())).getValue().getName()));
			int q=myTrip.getPosition(myOsmReader.getNodes().getPosition(((ALGOPlacemarkNode)myGraph.getNode(algoEdge.getSink())).getValue().getName()));
	
			// adding Items if not already existing
			if (p==-1){
			myTrip.addTripItem(myOsmReader.getNodes().getPosition(((ALGOPlacemarkNode)myGraph.getNode(algoEdge.getSource())).getValue().getName()));
//			Log.i(TAG, "Added to trip: "+String.valueOf((myOsmReader.getNodes().getPosition(((ALGOPlacemarkNode)myGraph.getNode(algoEdge.getSource())).getValue().getName()))));
			myTrip.addCheckItem(false);
			}
			
			if (q==-1){
			myTrip.addTripItem(myOsmReader.getNodes().getPosition(((ALGOPlacemarkNode)myGraph.getNode(algoEdge.getSink())).getValue().getName()));
//			Log.i(TAG, "Added to trip: "+String.valueOf((myOsmReader.getNodes().getPosition(((ALGOPlacemarkNode)myGraph.getNode(algoEdge.getSink())).getValue().getName()))));
			myTrip.addCheckItem(false);
			}
			
			

		}
	
	  }
	  return myTrip;
	}
	
	
	public Trip search(int startNodeID, int targetNodeID, ALGOGraph myGraph, CustomNodes cn, Trip myTrip, MyLocationOverlay myLocOverlay){

		ALGONode startNode=myGraph.getNode(startNodeID);
//		Log.i(TAG, "StartNodeID" + String.valueOf(startNodeID));
		ALGONode targetNode=myGraph.getNode(targetNodeID);
//		Log.i(TAG, "TargetNodeID: " + String.valueOf(targetNodeID));  
	      
	    ((SearchableGraph)myGraph).initSearch(startNode, targetNode);
		while (!((SearchableGraph)myGraph).solutionFound() &&
				!((SearchableGraph)myGraph).noSolution()) {
			
				((SearchableGraph)myGraph).nextStep();
			 
				
			}

			
			
		
	    
	    
	  if (((SearchableGraph)myGraph).solutionFound()) {
//		  Log.i(TAG, "Solution Found");
		  
		
		ALGOTrip trip = ((SearchableGraph)myGraph).computeShortestPath(startNode, targetNode);
		
		String TAG="URNav";
		for (ALGOEdge algoEdge : trip) {
//			Log.i(TAG, "yxc "+((ALGOPlacemarkNode)myGraph.getNode(algoEdge.getSource())).getValue().getName() + " -> " + ((ALGOPlacemarkNode)myGraph.getNode(algoEdge.getSink())).getValue().getName());
			
			// checking if Item is already in the Trip by looking up its ID/ Name in the ArrayList of Trip; returns -1 if not found
			int p=myTrip.getPosition(cn.getPosition(((ALGOPlacemarkNode)myGraph.getNode(algoEdge.getSource())).getValue().getName()));
			int q=myTrip.getPosition(cn.getPosition(((ALGOPlacemarkNode)myGraph.getNode(algoEdge.getSink())).getValue().getName()));
	
			// adding Items if not already existing
			if (p==-1){
			myTrip.addTripItem(cn.getPosition(((ALGOPlacemarkNode)myGraph.getNode(algoEdge.getSource())).getValue().getName()));
//			Log.i(TAG, "Added to trip: "+String.valueOf((myOsmReader.getNodes().getPosition(((ALGOPlacemarkNode)myGraph.getNode(algoEdge.getSource())).getValue().getName()))));
			myTrip.addCheckItem(false);
			}
			
			if (q==-1){
			myTrip.addTripItem(cn.getPosition(((ALGOPlacemarkNode)myGraph.getNode(algoEdge.getSink())).getValue().getName()));
//			Log.i(TAG, "Added to trip: "+String.valueOf((myOsmReader.getNodes().getPosition(((ALGOPlacemarkNode)myGraph.getNode(algoEdge.getSink())).getValue().getName()))));
			myTrip.addCheckItem(false);
			}
			
			

		}
	
	  }
	  return myTrip;
	}
	public int getClosestItem(ALGOGraph myGraph, OsmReader myOsmReader, Trip myTrip, MyLocationOverlay myLocOverlay){
		Location myLocation=myLocOverlay.getLastFix();
		int i;
		int closestItem=0;
		float closestDistance=9999;
		for (i=0; i<myOsmReader.getNodes().getSize(); i++){
			Location checkLocation=new Location("x");
			checkLocation.setLatitude(myOsmReader.getNodes().getDlati(i));
			checkLocation.setLongitude(myOsmReader.getNodes().getDlongi(i));
			float distance=myLocation.distanceTo(checkLocation);
			if (distance < closestDistance) {
				closestDistance=distance;
				closestItem = i;
//				Log.i(TAG, "Closest is: "+ String.valueOf(i));
			}
			
			
		}
		return closestItem;
		
	}

}
